/*
 * Properties.java
 *
 * Created on 27 de septiembre de 2006, 10:29 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package inc.jmc.cyberboard.common.properties;

import java.io.FileInputStream;

import org.apache.log4j.Logger;

/**
 *
 * @author JOlguin01
 */
public class DBConnection {
    
    private String servidor;
    private String usuario;
    private String password;
    private String driver;
    private String dbName;
	private String url;
	private String puerto;
    
    static Logger logger = Logger.getLogger(DBConnection.class.getName());

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }


    public String getDbName() {
        return dbName;
    }


    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setServidor(String servidor) {
        this.servidor = servidor;
    }

    public String getServidor() {
        return servidor;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getUsuario() {
        return usuario;
    }


    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getDriver() {
        return driver;
    }

    /** Creates a new instance of Properties */
    private DBConnection() {
        java.util.Properties prp=new java.util.Properties ();
	try{
            
            
            java.io.File file1=new java.io.File("");
            String path=file1.getCanonicalPath();
            String div=(System.getProperty("path.separator").equals(";"))?"\\":"/";
            String archivo=path+div+"opciones"+div+"dbconnection.properties";
            //String archivo="C:\\CyberCoffeeNet\\"+"opciones"+div+"dbconnection.properties";
            FileInputStream fis=new  FileInputStream(archivo);
            //prp.load(this.getClass().getResourceAsStream("dbconnection.properties"));
            prp.load(fis);
            this.driver=prp.getProperty("driver");
            this.servidor=prp.getProperty("servidor");
            this.usuario=prp.getProperty("usuario");
            this.password=prp.getProperty("password");
            this.dbName=prp.getProperty("dbname");
            this.puerto=prp.getProperty("puerto");
            if(this.getPassword()!=null)
            {
            	this.url="jdbc:mysql://"+
        		this.getServidor()+":"+puerto+"/"+
        		this.getDbName()+
        		"?user="+this.getUsuario()+
        		"&password="+this.getPassword();
            }
            else
            {
            	if(this.getPassword()==null || this.getPassword().equals(""))
            	{
            		this.url="jdbc:mysql://"+
            		this.getServidor()+":"+puerto+"/"+
            		this.getDbName()+
            		"?user="+this.getUsuario();
            	}
            }
            fis.close();
	}
	catch(java.io.IOException ex)
	{
              ex.printStackTrace();
	}
    }
    private static DBConnection conndb1=null;
    public static DBConnection getInstance()
    {
        if(conndb1==null||conndb1.getDbName().indexOf("test")>=0)
        {
            conndb1=new DBConnection();
        }
        return conndb1;
    }
    public static DBConnection getInstanceTest()
    {
        if(conndb1==null||conndb1.getDbName().indexOf("ccnetdb")>=0)
        {
            conndb1=new DBConnection();
            conndb1.setDbName("test");
            
        }
        return conndb1;
    }
    public static String calculaURL(DBConnection conn,String server)
    {
    	String ret="jdbc:mysql://"+
		server+":"+conn.getPuerto()+"/"+
		conn.getDbName()+
		"?user="+conn.getUsuario();
    	if(conn.getPassword()!=null)
        {
    		ret=ret+
    		"&password="+conn.getPassword();
        }
        
    	
    	return ret;
    	
    	/*return "jdbc:mysql://"+
		server+":"+conn.getPuerto()+"/"+
		conn.getDbName()+
		"?user="+conn.getUsuario()+
		"&password="+conn.getPassword();*/
    }
    public String getURLbasicDBMysql()
    {
    	String ret="jdbc:mysql://"+
		this.getServidor()+":"+this.puerto+"/"+
		"mysql"+
		"?user="+this.getUsuario();
    	if(this.getPassword()!=null)
        {
    		ret=ret+
    		"&password="+this.getPassword();
        }
    	
    	return ret;
    	/*return "jdbc:mysql://"+
		this.getServidor()+":"+this.puerto+"/"+
		"mysql"+
		"?user="+this.getUsuario()+
		"&password="+this.getPassword();*/
    	
    }


	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}


	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}


	/**
	 * @return the puerto
	 */
	public String getPuerto() {
		return puerto;
	}


	/**
	 * @param puerto the puerto to set
	 */
	public void setPuerto(String puerto) {
		this.puerto = puerto;
	}
    
}
